package com.wolfcode.contract.service.impl;

import java.util.List;
import java.util.Map;

import com.wolfcode.common.utils.DateUtils;
import com.wolfcode.common.utils.SecurityUtils;
import com.wolfcode.common.utils.StringUtils;
import com.wolfcode.contract.domain.info.ContractInfo;
import lombok.SneakyThrows;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.wolfcode.contract.mapper.ContractMapper;
import com.wolfcode.contract.domain.Contract;
import com.wolfcode.contract.service.IContractService;
import org.springframework.util.Assert;

/**
 * 合同信息Service业务层处理
 * 
 * @author wolfcode
 * @date 2022-12-10
 */
@Service
public class ContractServiceImpl implements IContractService 
{
    @Autowired
    private ContractMapper contractMapper;

    /**
     * 查询合同信息
     * 
     * @param id 合同信息主键
     * @return 合同信息
     */
    @Override
    public Contract selectContractById(Long id)
    {
        return contractMapper.selectContractById(id);
    }

    /**
     * 查询合同信息列表
     * 
     * @param contract 合同信息
     * @return 合同信息
     */
    @Override
    public List<Contract> selectContractList(Contract contract)
    {
        return contractMapper.selectContractList(contract);
    }

    /**
     * 新增合同信息
     * 
     * @param map 合同信息
     * @return 结果
     */
    @SneakyThrows
    @Override
    public int insertContract(Map map)
    {
        String fileName = (String) map.get("fileName");
        ContractInfo contractInfo = (ContractInfo) map.get("contractInfo");

        Assert.notNull(contractInfo,"参数异常");
        Assert.notNull(contractInfo.getCustomerId(),"未选择客户");
        Assert.state(!StringUtils.isEmpty(contractInfo.getContractName())
            && contractInfo.getContractName().length() <= 100,"合同名称不合法");
        Assert.notNull(contractInfo.getAmounts(),"未填金额");
        Assert.state(contractInfo.getStartDate().compareTo(contractInfo.getEndDate()) == -1,
                "日期不合法");
        Contract contract = new Contract();
        BeanUtils.copyProperties(contractInfo,contract);
        contract.setAffixSealState(0);
        contract.setAuditState(0);
        contract.setNullifyState(0);
        contract.setAppendix(fileName);
        contract.setInputUser(SecurityUtils.getLoginUser().getUsername());
        contract.setInputTime(DateUtils.getNowDate());
        System.out.println(contract);
        return contractMapper.insertContract(contract);
    }

    /**
     * 修改合同信息
     * 
     * @param contractInfo 合同信息
     * @return 结果
     */
    @Override
    public int updateContract(ContractInfo contractInfo)
    {
        Assert.notNull(contractInfo,"参数异常");
        Assert.notNull(contractInfo.getFile(),"未上传文件");
        //String url = MinIoUtil.minIoUploadFile(contractInfo.getFile());
        Contract contract = contractMapper.selectContractById(contractInfo.getId());
        //判断状态
        Assert.state(contract.getAuditState() == 0,"只有审核中才可以修改");
        Assert.state(contract.getNullifyState() == 0,"只有未作废才可以修改");

        BeanUtils.copyProperties(contractInfo,contract);
        contract.setAffixSealState(0);
        contract.setNullifyState(0);
        //contract.setAppendix(url);
        contract.setInputUser(SecurityUtils.getUsername());
        contract.setInputTime(DateUtils.getNowDate());
        return contractMapper.updateContract(contract);
    }



    /**
     * 作废合同信息信息
     *
     * @param id 合同信息主键
     * @return 结果
     */
    @Override
    public int deleteContractById(Long id)
    {
        Assert.notNull(id,"参数异常");
        Contract contract = contractMapper.selectContractById(id);
//判断状态 作废
        Assert.state(contract.getAffixSealState() == 0,"只有未盖章才可以作废");
        Assert.state(contract.getNullifyState() == 0,"只有未作废才可以作废");
        //改状态为已作废
        contract.setNullifyState(-1);
        return contractMapper.updateContract(contract);
    }

    //审核通过
    @Override
    public int auditpass(ContractInfo contractInfo) {
        Assert.notNull(contractInfo,"参数异常");
        Contract contract = contractMapper.selectContractById(contractInfo.getId());
        Assert.state(contract.getAuditState() == 0,"只有审核中才可以审核通过");
        BeanUtils.copyProperties(contractInfo,contract);
        //审核通过
        contract.setAuditState(1);
        return contractMapper.updateContract(contract);
    }
    //审核b不通过
    @Override
    public int auditnopass(ContractInfo contractInfo) {
        Assert.notNull(contractInfo,"参数异常");
        Contract contract = contractMapper.selectContractById(contractInfo.getId());
        Assert.state(contract.getAuditState() == 0,"只有审核中才可以审核通过");
        BeanUtils.copyProperties(contractInfo,contract);
        //审核通过
        contract.setAuditState(0);
        return contractMapper.updateContract(contract);
    }
    //盖章
    @Override
    public int affix(ContractInfo contractInfo) {
        Assert.notNull(contractInfo,"参数异常");
        Contract contract = contractMapper.selectContractById(contractInfo.getId());
        Assert.state(contract.getAuditState() == 0,"只有审核通过才可以盖章");
        BeanUtils.copyProperties(contractInfo,contract);
        //盖章 是
        contract.setAffixSealState(1);
        return contractMapper.updateContract(contract);
    }
}
